package com.ehaoyao.produce;

import com.alibaba.fastjson.JSONObject;
import com.ehaoyao.config.RabbitMQConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * <dl>
 * <dt>OrderProduce</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2006</dd>
 * <dd>CreateDate: 2020/5/12</dd>
 * </dl>
 *
 * @author zhangyp
 */
@Component
public class RabbitmqProduce {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RabbitTemplate rabbitTemplate;


    public void sendMsg(String orderNo, BigDecimal price){
        Map<String,Object> map = new HashMap<>();
        map.put("orderNo",orderNo);
        map.put("price",price);
        rabbitTemplate.convertAndSend(RabbitMQConfig.Delay_Exchange_Name,RabbitMQConfig.Delay_routing_key,map,message -> {
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            message.getMessageProperties().setDelay(getRandom()*1000);   // 毫秒为单位，指定此消息的延时时长
            return message;
        });
        logger.info("orderNo:"+orderNo+",price:"+price+"发送到队列成功");
    }

    public static int getRandom() {
        return  (int)(Math.random()*10+1) *6;
    }

    public static void main(String[] args) {
        System.out.println(getRandom());
    }
}
